// hover
var comHoverOn = function () {
  var jqElem = $(this);
  jqElem.append(jq.hoverMenu);
  jq.hoverMenu.show();
  jqElem.addClass('active');
};
var comHoverOff = function () {
  var jqElem = $(this);
  jq.hoverMenu.hide();
  jqElem.removeClass('active');
};

// edit
var editComponent = function () {
  var jqClick      = $(this);
  var editIndex    = jqClick.attr('index');
  if (getCurrentIndex() == editIndex) return;

  var params = collectForm();
  $.get( '/make/edit/' + editIndex, params, projectLoaded );
};

// add
var addComponent = function () {
  var jqClick      = jq.hoverMenu.siblings('.title');
  var editIndex    = jqClick.attr('index');

  var params = collectForm();
  $.get( '/make/add/' + editIndex, params, projectLoaded );
}

// movedown
var movedownComponent = function () {
  var jqClick      = jq.hoverMenu.siblings('.title');
  var editIndex    = jqClick.attr('index');
  if (editIndex == jq.components.length -1) return;

  var params = {};
  params.currentIndex = getCurrentIndex();

  $.get( '/make/movedown/' + editIndex, params, projectLoaded );
}

// moveup
var moveupComponent = function () {
  var jqClick      = jq.hoverMenu.siblings('.title');
  var editIndex    = jqClick.attr('index');
  if (editIndex == 0) return;

  var params = {};
  params.currentIndex = getCurrentIndex();

  $.get( '/make/moveup/' + editIndex, params, projectLoaded );
}

// remove
var removeComponent = function () {
  var jqClick      = jq.hoverMenu.siblings('.title');
  var editIndex    = jqClick.attr('index');

  var params = collectForm();
  $.get( '/make/remove/' + editIndex, params, projectLoaded );
}

// register
events.registerComs = function () {
  jq.components = $('.component');
  var jqTitles = jq.components.find('.title');
  jq.components.hover(comHoverOn, comHoverOff);

  // edit
  jqTitles.click( editComponent );
  
  // create
  jq.hoverMenu.find('.add').click( addComponent );

  // moveup
  jq.hoverMenu.find('.moveup').click( moveupComponent );
  
  // movedown
  jq.hoverMenu.find('.movedown').click( movedownComponent );
  
  // moveup
  jq.hoverMenu.find('.moveup').click( moveupComponent );
  
  // remove
  jq.hoverMenu.find('.remove').click( removeComponent );
};
